snippet sympy_solve "slove linear equations" b
from sympy import *

v1, v2, v3, v4 = symbols("v1:5")
vab, vi, vo, vs = symbols("vab vi vo vs")
i1, i2, i3, i4 = symbols("i1:5")
R1, R2, R3, R4, Rf = symbols("R1:5 Rf")

eq1 = $1

#ans = solve([eq1, eq2, eq3, eq4], [v1, v2, v3, v4])
#ans = solve([eq1, eq2, eq3], [v1, v2, v3])
#ans = solve([eq1, eq2], [v1, v2])
ans = solve([eq1], [v1])

pprint(solve(ans))
endsnippet

snippet sympy_diff "diff" b
from sympy import *
from sympy.abc import *

eq1 = $1

pprint(diff(eq1, (t)))
endsnippet

snippet sympy_integrate "integrate" b
from sympy import *
from sympy.abc import *

eq1 = ${1:(1 / (100 * 1e-6)) * 50 * sin(120 * pi * t) * 1e-3}

pprint(integrate(eq1, (t, 0, 1e-3)).evalf())
endsnippet

snippet sympy_dsolve "solve differential equations" b
from sympy import *

x = symbols("x")
y = Function('y')
#y = symbols("y", cls=Function)

# eq1 = diff(y(x), x) - 2 * x
eq1 = y(x).diff(x) - 2 * x
ics = None
# ics = {y(0) : 1}

pprint(dsolve(eq1, ics = ics))
endsnippet

snippet sympy_polar2rectangular "phasor representation"
def p2r(amp, angle, mode="radian"):
	# convert polar form to rectangular form(z = x + i*y)
    if mode == "radian":
        return amp * (cos(angle) + I * sin(angle))
    else:
        return amp * (cos(math.radians(angle)) + I * sin(math.radians(angle)))
endsnippet

snippet sympy_rectangular2polar "Description" b
def r2p(real, imaginary):
    """
	convert rectangular form(z = x + i*y) to polar form
    Abs(ans)
    math.degrees(arg(ans))
    """
    z = real + I * imaginary
    magnitude = Abs(z)
    phase = math.degrees(arg(z))
    return magnitude, phase
endsnippet

snippet sympy_complex_info "show the magnitude and angel" b
def get_complex_info(z):
    """
    get complex number's magnitude and phase
    """
    print("real part      : ", re(z))
    print("imaginary part : ", im(z))
    print("magnitude      : ", Abs(z))
    print("phase(angle)   : ", math.degrees(arg(z)))
    print("\n")
endsnippet

snippet sympy_plot "plot function"
from sympy import *
from sympy.abc import *
from sympy.plotting import plot

p1 = plot(${1:x * x}, ${2:(x, -5, 5)}, show=False, legend=True, line_color='red')
#p2 = plot(x, (x, -5, 5), show=False, line_color='green')
#p1.extend(p2)
p1.show()
endsnippet

snippet sympy_parallel "calc parallel element"
def parallel(*args):
    ret = 0
    for arg in args:
        ret = 1/arg + ret
    return simplify(1/ret)
endsnippet
